(function() {
  
  function getScript(src) {
    document.write('<' + 'script src="' + src + '"' +
                   ' type="text/javascript"><' + '/script>');
  }
  getScript("http://maps.google.com/maps/api/js?sensor=false");
})();

function isMarkerVisible() {
   return (marker.getVisible());
}

function fillUrls() {
   marker.setPosition(map.getCenter());
   var markerlat = null;
   var markerlng = null;
   if (isMarkerVisible()) {
      markerlat = marker.getPosition().lat();
      markerlng = marker.getPosition().lng();
   }
   var maptype = (map.getMapTypeId() == 'roadmap' ? 'map' : 'sat');
   fillUrlsCommon(map.getCenter().lat(), map.getCenter().lng(), map.getZoom(), maptype, markerlat, markerlng);
}

function processMarker(isGPS) {
   if (isGPS) {
      if (marker.getVisible()) {
         marker.setVisible(false);
      }
      gps_coords = new google.maps.LatLng(gps_lat, gps_lng);
      marker.setPosition(gps_coords);
      marker.setVisible(true);
      map.setCenter(gps_coords);
   } else {
      if (marker.getVisible()) {
         marker.setVisible(false);
      } else {
         marker.setPosition(map.getCenter());
         marker.setVisible(true);
      }
   }
   fillUrls();
   markerButtons(isGPS);
}

function UpControl(controlDiv, map) {
  controlDiv.style.padding = '5px';
  // Set CSS for the control border
  var controlUI = document.createElement('DIV');
  controlUI.style.backgroundColor = 'white';
  controlUI.style.borderStyle = 'solid';
  controlUI.style.borderWidth = '2px';
  controlUI.style.cursor = 'pointer';
  controlUI.style.textAlign = 'center';
  controlUI.title = 'Click to UP the maps';
  controlDiv.appendChild(controlUI);
  // Set CSS for the control interior
  var controlText = document.createElement('DIV');
  controlText.style.fontFamily = 'Arial,sans-serif';
  controlText.style.fontSize = '22px';
  controlText.style.paddingLeft = '4px';
  controlText.style.paddingRight = '4px';
  controlText.innerHTML = '&nbsp;&nbsp;UP&nbsp;&nbsp;';
  controlUI.appendChild(controlText);
  google.maps.event.addDomListener(controlUI, 'click', function() {
    map.panBy(0, -panOffset);
    fillUrls();
  });
}

function DownControl(controlDiv, map) {
   controlDiv.style.padding = '5px';
   // Set CSS for the control border
   var controlUI = document.createElement('DIV');
   controlUI.style.backgroundColor = 'white';
   controlUI.style.borderStyle = 'solid';
   controlUI.style.borderWidth = '2px';
   controlUI.style.cursor = 'pointer';
   controlUI.style.textAlign = 'center';
   controlUI.title = 'Click to Down the maps';
   controlDiv.appendChild(controlUI);
   // Set CSS for the control interior
   var controlText = document.createElement('DIV');
   controlText.style.fontFamily = 'Arial,sans-serif';
   controlText.style.fontSize = '22px';
   controlText.style.paddingLeft = '4px';
   controlText.style.paddingRight = '4px';
   controlText.innerHTML = 'DOWN';
   controlUI.appendChild(controlText);
   google.maps.event.addDomListener(controlUI, 'click', function() {
     map.panBy(0, panOffset);
     fillUrls();
   });
 }
 
function LeftControl(controlDiv, map) {
   controlDiv.style.padding = '5px';
   // Set CSS for the control border
   var controlUI = document.createElement('DIV');
   controlUI.style.backgroundColor = 'white';
   controlUI.style.borderStyle = 'solid';
   controlUI.style.borderWidth = '2px';
   controlUI.style.cursor = 'pointer';
   controlUI.style.textAlign = 'center';
   controlUI.title = 'Click to Left the maps';
   controlDiv.appendChild(controlUI);
   // Set CSS for the control interior
   var controlText = document.createElement('DIV');
   controlText.style.fontFamily = 'Arial,sans-serif';
   controlText.style.fontSize = '22px';
   controlText.style.paddingLeft = '4px';
   controlText.style.paddingRight = '4px';
   controlText.innerHTML = 'LEFT';
   controlUI.appendChild(controlText);
   google.maps.event.addDomListener(controlUI, 'click', function() {
     map.panBy(-panOffset, 0);
     fillUrls();
   });
 }

function RightControl(controlDiv, map) {
   controlDiv.style.padding = '5px';
   // Set CSS for the control border
   var controlUI = document.createElement('DIV');
   controlUI.style.backgroundColor = 'white';
   controlUI.style.borderStyle = 'solid';
   controlUI.style.borderWidth = '2px';
   controlUI.style.cursor = 'pointer';
   controlUI.style.textAlign = 'center';
   controlUI.title = 'Click to Right the maps';
   controlDiv.appendChild(controlUI);
   // Set CSS for the control interior
   var controlText = document.createElement('DIV');
   controlText.style.fontFamily = 'Arial,sans-serif';
   controlText.style.fontSize = '22px';
   controlText.style.paddingLeft = '4px';
   controlText.style.paddingRight = '4px';
   controlText.innerHTML = 'RIGHT';
   controlUI.appendChild(controlText);
   google.maps.event.addDomListener(controlUI, 'click', function() {
     map.panBy(panOffset, 0);
     fillUrls();
   });
 }

function ZoomInControl(controlDiv, map) {
   controlDiv.style.padding = '5px';
   // Set CSS for the control border
   var controlUI = document.createElement('DIV');
   controlUI.style.backgroundColor = 'white';
   controlUI.style.borderStyle = 'solid';
   controlUI.style.borderWidth = '2px';
   controlUI.style.cursor = 'pointer';
   controlUI.style.textAlign = 'center';
   controlUI.title = 'Click to Zoom In the maps';
   controlDiv.appendChild(controlUI);
   // Set CSS for the control interior
   var controlText = document.createElement('DIV');
   controlText.style.fontFamily = 'Arial,sans-serif';
   controlText.style.fontSize = '22px';
   controlText.style.paddingLeft = '4px';
   controlText.style.paddingRight = '4px';
   controlText.innerHTML = 'Zoom In';
   controlUI.appendChild(controlText);
   google.maps.event.addDomListener(controlUI, 'click', function() {
     map.setZoom(map.getZoom() + 1);
     fillUrls();
   });
 }

function ZoomOutControl(controlDiv, map) {
   controlDiv.style.padding = '5px';
   // Set CSS for the control border
   var controlUI = document.createElement('DIV');
   controlUI.style.backgroundColor = 'white';
   controlUI.style.borderStyle = 'solid';
   controlUI.style.borderWidth = '2px';
   controlUI.style.cursor = 'pointer';
   controlUI.style.textAlign = 'center';
   controlUI.title = 'Click to Zoom In the maps';
   controlDiv.appendChild(controlUI);
   // Set CSS for the control interior
   var controlText = document.createElement('DIV');
   controlText.style.fontFamily = 'Arial,sans-serif';
   controlText.style.fontSize = '22px';
   controlText.style.paddingLeft = '4px';
   controlText.style.paddingRight = '4px';
   controlText.innerHTML = 'Zoom Out';
   controlUI.appendChild(controlText);
   google.maps.event.addDomListener(controlUI, 'click', function() {
     map.setZoom(map.getZoom() - 1);
     fillUrls();
   });
 }

function RoadmapControl(controlDiv, map) {
   controlDiv.style.padding = '5px';
   // Set CSS for the control border
   var controlUI = document.createElement('DIV');
   controlUI.style.backgroundColor = 'white';
   controlUI.style.borderStyle = 'solid';
   controlUI.style.borderWidth = '2px';
   controlUI.style.cursor = 'pointer';
   controlUI.style.textAlign = 'center';
   controlUI.title = 'Click to Roadmap In the maps';
   controlDiv.appendChild(controlUI);
   // Set CSS for the control interior
   var controlText = document.createElement('DIV');
   controlText.style.fontFamily = 'Arial,sans-serif';
   controlText.style.fontSize = '22px';
   controlText.style.paddingLeft = '4px';
   controlText.style.paddingRight = '4px';
   controlText.innerHTML = 'Roadmap';
   controlUI.appendChild(controlText);
   google.maps.event.addDomListener(controlUI, 'click', function() {
     map.setMapTypeId(google.maps.MapTypeId.ROADMAP);
     fillUrls();
   });
 }

function HybridControl(controlDiv, map) {
   controlDiv.style.padding = '5px';
   // Set CSS for the control border
   var controlUI = document.createElement('DIV');
   controlUI.style.backgroundColor = 'white';
   controlUI.style.borderStyle = 'solid';
   controlUI.style.borderWidth = '2px';
   controlUI.style.cursor = 'pointer';
   controlUI.style.textAlign = 'center';
   controlUI.title = 'Click to Hybrid the maps';
   controlDiv.appendChild(controlUI);
   // Set CSS for the control interior
   var controlText = document.createElement('DIV');
   controlText.style.fontFamily = 'Arial,sans-serif';
   controlText.style.fontSize = '22px';
   controlText.style.paddingLeft = '4px';
   controlText.style.paddingRight = '4px';
   controlText.innerHTML = 'Hybrid';
   controlUI.appendChild(controlText);
   google.maps.event.addDomListener(controlUI, 'click', function() {
      map.setMapTypeId(google.maps.MapTypeId.HYBRID);
      fillUrls();
   });
 }


function initialize() {

   initMapCanvasSize();

   var lat = getParameter('lat');
   if (lat == null || isNaN(lat))
      lat = def_lat;
   var lng = getParameter('lng');
   if (lng == null || isNaN(lat))
      lng = def_lng;
   var z = parseInt(getParameter('z'));
   if (z == null || isNaN(z))
      z = def_zoom;

   var mapType = getParameter('t');
   if (mapType == null)
      mapType = google.maps.MapTypeId.ROADMAP;
   else 
      if (mapType == 'sat')
        mapType = google.maps.MapTypeId.HYBRID;
      else 
        mapType = google.maps.MapTypeId.ROADMAP;
        
   var mlat = getParameter('mlat');
   var mlng = getParameter('mlng');
   var markerCoordinates = null;
   if (mlat != null && !isNaN(mlat) && mlng != null && !isNaN(mlng)) {
      lng = mlng;
      lat = mlat;
      markerCoordinates = new google.maps.LatLng(mlat, mlng);
   }
   
   var mapCenter = new google.maps.LatLng(lat, lng);

   
   var myOptions = {
     zoom: z,
     center: mapCenter,
     mapTypeId: mapType,
     scaleControl: true,
     scaleControlOptions: {
         position: google.maps.ControlPosition.BOTTOM_LEFT
     },
     mapTypeControl: false,
     panControl: false,
     zoomControl: false,
     streetViewControl: false,
     disableDoubleClickZoom: true,
     draggable: false,
     scrollwheel: false,
   };
   map = new google.maps.Map(document.getElementById("map_canvas"),
       myOptions);

   

   // Create the DIV to hold the control and call the UpControl() constructor
   // passing in this DIV.
   var upControlDiv = document.createElement('DIV');
   var upControl = new UpControl(upControlDiv, map);
   upControlDiv.index = 1;
   map.controls[google.maps.ControlPosition.TOP_CENTER].push(upControlDiv);

   // Create the DIV to hold the control and call the DownControl() constructor
   // passing in this DIV.
   var downControlDiv = document.createElement('DIV');
   var downControl = new DownControl(downControlDiv, map);
   downControlDiv.index = 1;
   map.controls[google.maps.ControlPosition.BOTTOM_CENTER].push(downControlDiv);

   // Create the DIV to hold the control and call the LeftControl() constructor
   // passing in this DIV.
   var leftControlDiv = document.createElement('DIV');
   var leftControl = new LeftControl(leftControlDiv, map);
   leftControlDiv.index = 2;
   map.controls[google.maps.ControlPosition.LEFT_CENTER].push(leftControlDiv);

   // Create the DIV to hold the control and call the RightControl() constructor
   // passing in this DIV.
   var rightControlDiv = document.createElement('DIV');
   var rightControl = new RightControl(rightControlDiv, map);
   rightControlDiv.index = 3;
   map.controls[google.maps.ControlPosition.RIGHT_CENTER].push(rightControlDiv);

   // Create the DIV to hold the control and call the ZoomInControl() constructor
   // passing in this DIV.
   var zoomInControlDiv = document.createElement('DIV');
   var zoomInControl = new ZoomInControl(zoomInControlDiv, map);
   zoomInControlDiv.index = 4;
   map.controls[google.maps.ControlPosition.TOP_LEFT].push(zoomInControlDiv);

   // Create the DIV to hold the control and call the ZoomOutControl() constructor
   // passing in this DIV.
   var zoomOutControlDiv = document.createElement('DIV');
   var zoomOutControl = new ZoomOutControl(zoomOutControlDiv, map);
   zoomOutControlDiv.index = 5;
   map.controls[google.maps.ControlPosition.LEFT_TOP].push(zoomOutControlDiv);


   // Create the DIV to hold the control and call the RoadmapControl() constructor
   // passing in this DIV.
   var roadmapControlDiv = document.createElement('DIV');
   var roadmapControl = new RoadmapControl(roadmapControlDiv, map);
   roadmapControlDiv.index = 5;
   map.controls[google.maps.ControlPosition.TOP_RIGHT].push(roadmapControlDiv);

   // Create the DIV to hold the control and call the HybridControl() constructor
   // passing in this DIV.
   var hybridControlDiv = document.createElement('DIV');
   var hybridControl = new HybridControl(hybridControlDiv, map);
   hybridControlDiv.index = 5;
   map.controls[google.maps.ControlPosition.RIGHT_TOP].push(hybridControlDiv);

   
   // MARKER
   marker = new google.maps.Marker({
      map: map,
      draggable: false,
      position: markerCoordinates,
      visible: (markerCoordinates != null ? true : false)
    });
    

    // init methods
    fillUrls();
    initMarkerButtons(marker.getVisible());
    pageOnLoad();
 }

function resizeMap() {
   google.maps.event.trigger(map, 'resize');
   fillUrls();
}